package com.ts.content.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ts.common.core.utils.Result;
import com.ts.common.security.dto.UserLoginDto;
import com.ts.content.entity.FollowEntity;
import com.ts.content.entity.UserEntity;
import com.ts.content.service.FollowService;
import com.ts.content.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private FollowService followService;

    /**
     * 根据手机号查询用户个人信息
     * @param phone
     * @return UserEntity 返回的是用户信息， null表示用户不存在
     */
    @GetMapping("/getUserByPhone/{phone}")
    public UserEntity getUserByPhone(@PathVariable String phone){
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);

        UserEntity entity = userService.getOne(queryWrapper);

        return entity;
    }

    @PostMapping("/addUser")
    public UserEntity addUser(@RequestBody UserEntity user){
        user.setCreateTime(new Date());
        userService.save(user);
        return user;
    }

    @PostMapping("save")
    public UserEntity save(@RequestBody UserEntity userEntity){
        boolean save = userService.save(userEntity);
        return userEntity;
    }
    /**
     * @Author:zxc
     * @Description: 修改用戶头像
     */
    @PostMapping("/currentUserInfo")
    public boolean currentUserInfo(@RequestBody UserEntity userEntity){
        boolean b = userService.updateById(userEntity);
        return b;
    }

   /**
    * @Author:zxc
    * @Description: 修改用户名/性别/生日/个性签名/标签
    */
    @PostMapping("userInfoEdit")
    public boolean userInfoEdit(@RequestBody UserEntity userEntity, UserLoginDto userLoginDto){
        userEntity.setId(userLoginDto.getUserId());
        boolean b = userService.updateById(userEntity);
        return b;
    }

    /**
     * 当前用已经关注了，调用取消关注的接口
     * @param userLoginDto
     * @return
     */
    @PostMapping("/cancelFollow")
    public Result cancelFollow(UserLoginDto userLoginDto){
        return new Result();
    }

    /**
     * 关注用户
     * @param userLoginDto
     * @return
     */
    @PostMapping("/addFollow")
    public Result addFollow(UserLoginDto userLoginDto,@RequestBody FollowEntity follow){
        followService.follower(userLoginDto.getUserId(),follow.getUserId());
        Boolean flag = followService.isFollow(userLoginDto.getUserId(), follow.getUserId());
        return new Result().Viewok(flag);
    }
}
